<template>
  <!-- 登录 -->
  <div class="login">
    <span class="goout" @click="goout()">退出登录</span>
    <div class="login_top">
      <img src="../../static/image/registerback.png" alt />
    </div>
    <div class="login_logo">
      <img src="../../static/image/logo.png" alt />
      <div>税东家(国际)税务商学院</div>
    </div>
    <div class="login_from">
      <div>
        <img src="../../static/image/username.png" alt />
        <input
          type="text"
          name="user"
          value
          bindinput="getUser"
          placeholder="用户名"
          v-model="user"
        />
      </div>
      <div>
        <img src="../../static/image/password.png" alt />
        <input type="password" placeholder="密码" v-model="password" />
      </div>
      <button class="submit" @click="getMethod()">登录</button>
    </div>
  </div>
</template>
<script>
import axios from "axios";
import { Account, codeData } from "../api/api";
import querystring from "query-string";
import crypto from "crypto";
import { Toast } from "mint-ui";

export default {
  name: "Login",
  data() {
    return {
      user: "",
      password: "",
    };
  },
  methods: {
    //点击登录
    getMethod() {
      const md5 = crypto.createHash("md5");
      md5.update(this.password);
      const md5pwd = md5.digest("hex");
      if (this.user == "") {
        Toast("用户名不能为空");
        return false;
      } else if (this.password == "") {
        Toast("密码不能为空");
        return false;
      } else if (this.password.length < 8) {
        Toast("密码不能小于8位");
        return false;
      } else {
        var params = {
          accountNumber: this.user,
          password: md5pwd.toLowerCase(),
        };
        var params = querystring.stringify(params);
        Account(params).then((res) => {
          if (res.data.code == 1) {
            Toast({
              message: "登录成功",
              iconClass: "icon icon-success",
            });
            var token = res.data.data.token;
            var user = res.data.data.user;
            if (user.roleCode == "branch") {
              this.$router.push("/Branch/homepage");
            } else if (user.roleCode == "center") {
              this.$router.push("/Generalhospital/homepage");
            }
            localStorage.setItem("user", JSON.stringify(user));
            localStorage.setItem("token", JSON.stringify(token));
          } else {
            Toast(res.data.message);
          }
        });
      }
    },
    goout() {
      localStorage.clear();
      this.$router.push({ path: "/" });
    },
    getDa(codeVavl) {
      let params = {
        code: codeVavl,
      };
      params = querystring.stringify(params);
      let a = localStorage.getItem("token");
      codeData(params).then((res) => {
        if (res.data.code !== 0) {
          localStorage.setItem("token", JSON.stringify(res.data.data.token));
          let userList = res.data.data.user;
          localStorage.setItem("user", JSON.stringify(userList));
          if (localStorage.getItem("user")) {
            let newUser = JSON.parse(localStorage.getItem("user"));
            if (newUser.roleCode === "user") {
              this.$router.push({ name: "Usehom" });
            }
            if (newUser.roleCode === "center") {
              this.$router.push({ name: "Ghome" });
            }
            if (newUser.roleCode === "salesman") {
              this.$router.push({ name: "Sahomepage" });
            }
            if (newUser.roleCode === "branch") {
              this.$router.push({ name: "Bhome" });
            }
          }
        } else {
          Toast(res.data.message);
        }
      });
    },
  },
  mounted() {},
  created() {
    if (this.$route.query.code) {
      localStorage.setItem("code", JSON.stringify(this.$route.query.code));
      if (localStorage.getItem("code")) {
        this.getDa(JSON.parse(localStorage.getItem("code")));
      }
    }
  },
};
</script>
<style lang="scss" scoped>
.login {
  width: 100%;
  height: auto;
}
.login_top {
  width: 100%;
  height: 4.66rem;
  position: relative;
}
.login_top img {
  width: 100%;
  height: 4.66rem;
  position: absolute;
  top: 0;
  left: 0;
  z-index: -1;
}
.goout {
  position: absolute;
  font-size: 0.25rem;
  color: #cfcfcf;
  top: 0.6rem;
  z-index: 999;
  left: 0.4rem;
}
.login_logo {
  width: 100%;
  height: auto;
  display: flex;
  align-items: center;
  flex-direction: column;
  margin-top: -1.65rem;
}
.login_logo img {
  width: 2.1rem;
  height: 2.1rem;
  border-radius: 50%;
}
.login_logo div {
  font-size: 0.4rem;
  font-weight: bold;
  color: #212c67;
  margin-top: 0.36rem;
}

.login_from {
  width: 5.6rem;
  height: auto;
  margin: auto;
  margin-top: 0.89rem;
  display: flex;
  flex-direction: column;
  align-items: center;
}
.login_from div {
  width: 5.4rem;
  height: 0.82rem;
  background: #dfe2ee;
  border-radius: 0.4rem;
  display: flex;
  margin-bottom: 0.5rem;
  align-items: center;
}
.login_from img {
  width: 0.28rem;
  height: 0.35rem;
  margin-left: 0.37rem;
  margin-right: 0.19rem;
}
.login_from input {
  width: 4.3rem;
  height: 0.8rem;
  font-size: 0.3rem;
  padding-left: 0.1rem;
  box-sizing: border-box;
  background: #dfe2ee;
  border: none;
}
.login_from .submit {
  width: 5.6rem;
  height: 0.9rem;
  background: #ffb400;
  border-radius: 0.45rem;
  font-size: 0.34rem;
  color: #ffffff;
  border: none;
}
</style>